﻿using Adp;
using Microsoft.AspNetCore.Mvc;
using Yqd.Models;
using System.Linq;
using Yqd.Controllers.Filter;
using Adp.Common;
using System;
using Yqd.Core;
using System.Collections.Generic;
using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using Newtonsoft.Json.Linq;
using Yqd.Enm;

namespace Yqd.Controllers
{
    [Area("Yqd")]
    public class WxSpecialTargetController : Controller
    {
        YqdContext ctx;

        public WxSpecialTargetController(YqdContext ctx)
        {
            this.ctx = ctx;
        }

        [User]
        public ActionResult GetList()
        {
            var TaskID = Form.GetInt("TaskID");
            var TypeID = Form.GetInt("TypeID");
            var page = Form.GetInt("page", 1);
            var pagesize = Form.GetInt("pagesize", 0);
            var r = ctx.Yqd_SpecialTemplate.Where(it => it.IsGeneral || (it.TaskID == TaskID && it.TypeID == TypeID));
            var datas = ctx.Yqd_SpecialTarget.Where(it => it.TaskID == TaskID && it.TypeID == TypeID);
            var data = new List<object>();
            foreach (var d in datas)
            {
                if (d.Content.HasValue())
                {
                    var o = JObject.Parse(d.Content);
                    o["TargetID"] = d.TargetID;
                    data.Add(o);
                }
            }
            var totalcount = r.Count();
            if (pagesize > 0)
            {
                r = r.Skip((page - 1) * pagesize).Take(pagesize);
            }
            return this.ToJson(new
            {
                success = true,
                totalcount,
                Fields = r.OrderBy(it => it.Tactic).ToList().Select(it => new
                {
                    it.TemplateID,
                    it.FieldID,
                    it.Name
                }),
                data
            });
        }


        [User]
        public ActionResult Get()
        {
            var TargetID = Form.GetInt("TargetID");
            var target = ctx.Yqd_SpecialTarget.FirstOrDefault(it => it.TargetID == TargetID);
            if (target == null)
            {
                return this.ToJson(new
                {
                    success = true
                });
            }
            string TaskName = "", TypeName = "";
            JObject Targets = null;
            var task = ctx.Yqd_SpecialTask.FirstOrDefault(it => it.TaskID == target.TaskID);
            if (task != null) TaskName = task.Name;
            var type = ctx.Yqd_SpecialTaskType.FirstOrDefault(it => it.TypeID == target.TypeID);
            if (type != null) TypeName = type.Name;
            var field = ctx.Yqd_SpecialTemplate.Where(it => it.IsGeneral || (it.TaskID == target.TaskID && it.TypeID == target.TypeID));
            if (target.Content.HasValue())
            {
                Targets = JObject.Parse(target.Content);
            }
            var standard = ctx.Yqd_SpecialStandard.Where(it => it.TaskID == target.TaskID && it.TypeID == target.TypeID);
            return this.ToJson(new
            {
                success = true,
                TaskName,
                TypeName,
                Fields = field.OrderBy(it => it.Tactic).ToList().Select(it => new
                {
                    it.FieldID,
                    it.Name
                }),
                Targets,
                Standards = standard.OrderBy(it => it.Tactic).ToList().Select(it => new
                {
                    it.StandardID,
                    it.Content
                })
            });
        }
    }
}